<template>
	<div>
    <Breadcrumb :style="{margin: '15px 0',float: 'left'}">
      <BreadcrumbItem>首页</BreadcrumbItem>
      <BreadcrumbItem>会员中心</BreadcrumbItem>
      <BreadcrumbItem>普通会员</BreadcrumbItem>
    </Breadcrumb>
    <Content class="layer-content">
      <Form inline>
        <FormItem>
          <Input style="width: 400px" v-model="keywords" search enter-button placeholder="请输入关键字" @on-search="searchGet" @submit.native.prevent/>
        </FormItem>
        <FormItem>
					<Select style="width: 150px" placeholder="请选择户主关系" clearable @on-change="selectOption">
            <Option v-for="(item,index) in householdRoleArray" :value="item.roleId" :key="index">{{item.roleName}}</Option>
          </Select>
				</FormItem>
        <FormItem>
					<Select style="width: 150px" placeholder="审核状态" clearable @on-change="selectAudit">
            <Option :value="1">待审核</Option>
            <Option :value="2">已通过</Option>
            <Option :value="3">已驳回</Option>
          </Select>
				</FormItem>
        <FormItem>
					<Cascader style="width: 300px" placeholder="请选择地区" :data="regionArray" @on-change="regionSelector" :load-data="loadRegion"></Cascader>
				</FormItem>
        <FormItem>
          <Button @click="reset">重置</Button>
        </FormItem>
      </Form>
      <Table border :columns="columns" :data="dataGrid"></Table>
      <HigPager v-model="query" :total="pageTotal" :init="Init"></HigPager>
    </Content>
    <Modal :footer-hide="true" v-model="showRoleAuth" title="居(村)民详情" width="70%" @on-cancel="onCancel">
      <Authorize ref="refAuthorize" :dataBus="dataVo" :callbank="callEvent" :key="keyIndex"></Authorize>
    </Modal>
  </div>  
</template>
<script scope>
import Authorize from './authorize.vue'
export default {
  data () {
    return {
      pageTotal: 0,
      keyIndex: 0,
      dataGrid:[],
      regionArray: [],
      householdRoleArray: this.$store.page.getHomerole(),
      keywords: null,
      showRoleAuth: false,
      dataVo: {},
      query: {
        pageIndex: 1,
        pageSize: 15
      },
      columns: [
        {
          title: "用户姓名",
          key: "userName",
          width: 120
        },
        {
          title: "户主关系",
          key: "householdRoleText",
          width: 120
        },
        {
          title: "联系电话",
          key: "userMobile",
          width: 180
        },
        {
          title: "户号",
          key: "householdCode",
          width: 120
        },
        {
          title: "家庭住址",
          key: "homeAddress"
        },
        {
          title: "审核状态",
          key: "auditText",
          width: 120,
          render: (h, params) => {
            return h("div", [
              h("Tag",params.row.authText),
            ]);
          }
        },
        {
          title: "注册时间",
          key: "postTime",
          width: 150,
          render: (h, params) => {
            return h("div", this.$date(params.row.postTime,'yyyy-MM-dd HH:mm'));
          },
        },
        {
          title: "操作",
          key: "action",
          width: 120,
          render: (h, params) => {
            return h("nav", [
              h(
                "Button",
                {
                  props: {
                    type: "primary",
                    size: "small",
                  },
                  style: {
                    marginLeft: "10px",
                  },
                  on: {
                    click: () => {
                      this.auth(params.row)
                    },
                  },
                },
                "详情"
              )
            ])
          },
        },
      ]
    }
  },
  components: {
    Authorize
  },
  methods: {
    //授权
    auth(e){
      e.gender = this.$enum.gender(e.userSex)
      this.dataVo = e
      //this.$refs.refAuthorize.Init()
      this.showRoleAuth = true
    },
    onCancel() {
      this.keyIndex ++
    },
    callEvent(){
      this.showRoleAuth = false
      this.keyIndex ++
      this.Init()
    },
    //获取列表
    Init(){
      new this.$rest({
        gateway: 'agrobiz',
        service: 'agrobiz-uc',
        url: '/v1.0/villager/page',
        query: this.query,
        action: (res) => {
          if (res.data.code === 20000) {
            this.pageTotal = res.data.data.count
            this.dataGrid = res.data.data.list
            for(let item of this.dataGrid) {
              item.userName = item.userName ? item.userName : '匿名用户'
              item.authText = this.$enum.villagerStatus(item.authStatus)
              item.householdRoleText = this.$enum.householdRole(item.householdRole)
            }
          } else {
            this.$Message.error(res.data.msg)
          }
        }
      }).get()
     },
    regionInit() {
      new this.$rest({
				gateway: 'agrobiz',
        service: "agrobiz-uc",
        url: "/v1.0/plus/union/region/lazy",
				query: {
					equal: {
            regionId: "0"
          }
				},
				action: (res)=>{
					if(res.data.code === 20000){
						const data = res.data.data
						this.regionArray = data
					} else {
						this.$Message.error({
              content: res.data.msg
            })
					}
				}
			}).get()
    },
    loadRegion(item, callback) {
			item.loading = true
			new this.$rest({
				gateway: 'agrobiz',
        service: "agrobiz-uc",
        url: "/v1.0/plus/union/region/lazy",
				query: {
					equal: {
            regionId: item.id,
            regionLevel: 4
          }
				},
				action: (res)=>{
					if(res.data.code === 20000){
						const data = res.data.data
						item.children = data
						item.loading = false
            callback()
					} else {
						this.$Message.error({
              content: res.data.msg
            })
					}
				}
			}).get()
		},
    regionSelector(value) {
      if(!this.query.equal) this.query.equal = new Object()
      this.query.equal.townshipCode = value[value.length - 1]
      this.Init()
		},
    selectOption(roleId) {
      if(!this.query.equal) this.query.equal = new Object()
      this.query.equal.householdRole = roleId
      this.Init()
    },
    selectAudit(status) {
      if(!this.query.equal) this.query.equal = new Object()
      this.query.equal.authStatus = status
      this.Init()
    },
     //搜索
    searchGet() {
      let sqlLike = {
        fields: ["user_name","user_mobile","id_card_no"],
        keyword: this.keywords,
      }
      this.query.sqlLike = sqlLike
      this.query.pageIndex = 1
      this.Init()
    },
    //重置
    reset() {
      delete this.query.sqlLike
      this.keywords = null
      this.Init()
    },
  },
  created(){
    this.Init()
    this.regionInit()
  }
}
</script>
<style lang="less" scoped>
.imgs {
  width: 80%;
}
</style>